package com.vnp.cooking.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.vnp.cooking.R;
import com.vnp.cooking.UIcontrols.MyCircleImageView;
import com.vnp.cooking.bean.ObjComment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListCommentAdapter extends ArrayAdapter<ObjComment>{
	
	ArrayList<ObjComment> objComments;
	Context context;
	
	// khai bao imageloader
	DisplayImageOptions options = new DisplayImageOptions.Builder()
									.cacheInMemory(false)
									.cacheOnDisc(true)
									.displayer(new RoundedBitmapDisplayer(100))
									.displayer(new FadeInBitmapDisplayer(300))
									.build();
									
	
	public ListCommentAdapter(Context context,	ArrayList<ObjComment> objComments) {
		super(context, R.layout.list_comment_item, objComments);
		this.context = context;
		this.objComments = objComments;
	}
	
	
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View v, ViewGroup parent) {
		// khai bao view holder
		ViewHolderComment holder = null;
		
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.list_comment_item, parent, false);
			
			holder = new ViewHolderComment();
			
			holder.img_avatar_user = (MyCircleImageView) v.findViewById(R.id.img_avata_user);
			holder.txt_name_user = (TextView) v.findViewById(R.id.txt_name_user);
			holder.txt_time_ago_comment = (TextView) v.findViewById(R.id.txt_time_ago_comment);
			holder.txt_comment = (TextView) v.findViewById(R.id.txt_comment);
			// set tag holder
			v.setTag(holder);
		} else {
			holder = (ViewHolderComment) v.getTag();
		}
		
		// set gia tri cho cac phan tu
		ImageLoader.getInstance().displayImage(objComments.get(position).getAvataChef(), holder.img_avatar_user, options);
		holder.txt_name_user.setText(objComments.get(position).getNameChef());
		holder.txt_time_ago_comment.setText(objComments.get(position).getCreatedDateComment());
		holder.txt_comment.setText(objComments.get(position).getTxtComment());
		return v;
	}
	
	
	class ViewHolderComment {
		MyCircleImageView img_avatar_user;
		TextView txt_name_user;
		TextView txt_time_ago_comment;
		TextView txt_comment;
	}
	
	
	

}
